function wuyuan(R0, x, w) {
    const n = R0.length;
    const mm = R0[0].length;
    const m = mm / 2;
    const a = Array.from({length: n}, () => Array(m).fill(0));
    const b = Array.from({length: n}, () => Array(m).fill(0));

    for (let i = 0; i < n; i++) {
        for (let j = 0; j < m; j++) {
            a[i][j] = R0[i][j * 2];
            b[i][j] = R0[i][j * 2 + 1];
        }
    }

    const ap = Array(n).fill(0);
    const bp = Array(n).fill(0);

    for (let i = 0; i < ap.length; i++) {
        ap[i] = R0[i][0];
        bp[i] = R0[i][mm - 1];
    }

    const fah = x.length;
    const fal = x[0].length;
    const k = Array.from({length: n}, () => Array(m).fill(0));
    const index = [];
    let jstar = 0;

    for (let g = 0; g < fal; g++) {
        for (let i = 0; i < n; i++) {
            for (let j = 0; j < m; j++) {
                if (a[i][j] == b[i][j]) {
                    if (x[i][g] == a[i][j]) {
                        k[i][j] = 0;
                    } else {
                        const pp = Math.abs(x[i][g] - 0.5 * (ap[i] + bp[i])) - 0.5 * (bp[i] - ap[i]);
                        k[i][j] = Math.abs(x[i][g] - a[i][j]) / (pp - Math.abs(x[i][g] - a[i][j]));
                    }
                } else {
                    if (x[i][g] >= a[i][j] && x[i][g] <= b[i][j]) {
                        const p = Math.abs(x[i][g] - 0.5 * (a[i][j] + b[i][j])) - 0.5 * (b[i][j] - a[i][j]);
                        k[i][j] = -p / Math.abs(b[i][j] - a[i][j]);
                    } else {
                        const p = Math.abs(x[i][g] - 0.5 * (a[i][j] + b[i][j])) - 0.5 * (b[i][j] - a[i][j]);
                        const pp = Math.abs(x[i][g] - 0.5 * (ap[i] + bp[i])) - 0.5 * (bp[i] - ap[i]);
                        k[i][j] = p / (pp - p);
                    }
                }
            }
        }

        const kp = Array(m).fill(0);

        for (let j = 0; j < m; j++) {
            for (let i = 0; i < w.length; i++) {
                kp[j] += w[i] * k[i][j];
            }
        }

        let kpmaxIndex = 0;
        let kpmax = kp[0];
        let kpmin = kp[0];

        for (let i = 1; i < kp.length; i++) {
            if (kp[i] > kpmax) {
                kpmax = kp[i];
                kpmaxIndex = i;
            }
            if (kp[i] < kpmin) {
                kpmin = kp[i];
            }
        }

        index.push(kpmaxIndex);

        const avkpp = Array(m).fill(0);
        let avkppSum = 0;

        for (let i = 0; i < m; i++) {
            avkpp[i] = (kp[i] - kpmin) / (kpmax - kpmin);
            avkppSum += avkpp[i];
        }

        let jup = 0;

        for (let i = 0; i < m; i++) {
            jup += i * avkpp[i];
        }

        jstar = jup / avkppSum;
    }

    let result = '';

    switch (Math.round(jstar)) {
        case 0:
            result = '差';
            break;
        case 1:
            result = '中';
            break;
        case 2:
            result = '良';
            break;
        case 3:
            result = '优';
            break;
        default:
            result = '未知'; // 如果jstar不在0-3之间，返回未知
    }

    console.log(index);
    console.log(jstar);
    console.log(result);
    return result;
}

const R0 = [
    [0, 10, 11, 20, 21, 30, 31, 40],
    [0, 10, 11, 20, 21, 30, 31, 40],
    [0, 10, 11, 20, 21, 30, 31, 40],
    [0, 10, 11, 20, 21, 30, 31, 40]
];

const x = [
    [40, 40, 40, 40],
    [40, 40, 40, 40],
    [40, 40, 40, 40],
    [40, 40, 40, 40]
];

const w = [0.25, 0.25, 0.25, 0.25];

wuyuan(R0, x, w); // 输出评价结果
